package cn.tkb.org.controller;

import cn.tkb.basic.util.AjaxResult;
import cn.tkb.basic.util.PageQuery;
import cn.tkb.org.domain.Employee;
import cn.tkb.org.query.EmployeeQuery;
import cn.tkb.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    //新增和修改
    @RequestMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败！！！");
        }
    }
    //查询部门
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.QeuryById(id);
    }
    //删除部门
    @DeleteMapping("/{id}")
        public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }

    }
    //批量删除
    @PatchMapping
    public AjaxResult BatchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.BatchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.fail(e.getMessage());
        }
    }
    //查询所有部门
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.QeuryAll();
    }
    //高级查询和分页查询
    @PostMapping
    public PageQuery<Employee> queryPage(@RequestBody EmployeeQuery query){
       return employeeService.findPage(query) ;
    }
}
